<?php 
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Toji extends Controller{
	//开奖结果总详情
	//查询区间start-end单位(秒),不填写代表不限
	function index($stime=0,$etime=0,$reddata1=0,$reddata2=0,$redmold=0){
		model('user')->where('id',session('adminid'))->update(['addrs'=>13]);
        $announce = model('announce')->findstatus(2);
        $this->assign('announce',$announce);

        if($redmold){
			$arr = redckout($redmold);
			$cond = "";
			foreach ($arr as $k => $v) {
				if($k=="bets"){
					$cond.="and bets in (";
					foreach ($v as $item) {
						$cond.="{$item},";
					}
					$cond = substr($cond,0, strlen($cond)-1);
					$cond.=") ";
				}else{
					$cond.="and {$k} = {$v} ";
				}
			}
		}else{
			$cond = "";
		}

        if($reddata1 && $reddata2){
        	$this->getByDataTime($reddata1,$reddata2,$cond);
        }else{
        	$this->getByTime($stime,$etime,$cond);
        }

		return $this->fetch();
	}
	function getByTime($stime=0,$etime=0,$cond){
		
		if($stime){
        	$start = strtotime($stime);
        }else{
        	$start = 0;
        }

		$sql = "select * from profzb where orders_id in (select id from orders where order_id in (select id from ordered where times >= {$start}) {$cond})";
		if($etime){
			$end = strtotime($etime);
			$sql = "select * from profzb where orders_id in (select id from orders where order_id in (select id from ordered where times >= {$start} and times<={$end}) {$cond})";
		}
		$res = Db::query($sql);

		$this->assign("res",$res);
		
		//查询一共有多少期
		$sql = "select * from ordered where times >= {$start} group by datatime";
		if($etime){
			$sql = "select * from ordered where times >= {$start} and times<={$end} group by datatime";
		}
		$num = count(Db::query($sql));
		
		$this->assign("num",$num);
		$this->assign("data","日期 {$stime} ~ {$etime}");
	}
	function getByDataTime($start,$end,$cond){

		$sql = "select * from profzb where orders_id in (select id from orders where order_id in (select id from ordered where datatime >= {$start} and datatime<={$end} {$cond}))";
		$res = Db::query($sql);

		$this->assign("res",$res);

		$sql = "select * from ordered where datatime >= {$start} and datatime<={$end} group by datatime";
		$num = count(Db::query($sql));

		$this->assign("num",$num);
		$this->assign("data","期数 {$start} ~ {$end}");
	}
	//开奖结果总报表
	//who 查找谁的报表(1-7)
	function summary($stime=0,$etime=0,$reddata1=0,$reddata2=0,$redmold=0){
		if($redmold){
			$arr = redckout($redmold);
			$cond = "";
			foreach ($arr as $k => $v) {
				if($k=="bets"){
					$cond.="and bets in (";
					foreach ($v as $item) {
						$cond.="{$item},";
					}
					$cond = substr($cond,0, strlen($cond)-1);
					$cond.=") ";
				}else{
					$cond.="and {$k} = {$v} ";
				}
			}
		}else{
			$cond = "";
		}

		model('user')->where('id',session('adminid'))->update(['addrs'=>13]);
		$announce = model('announce')->findstatus(2);
        $this->assign('announce',$announce);

        $admin = db("user")->find(session('adminid'));
        $who = $admin['types'];
		

		

		//总报表
		//找到对应身份的旗下的所有会员
		$sql = "select id,username,name from user where topuser = ".session('adminid');
		$users = Db::query($sql);
		$lastPrice = 0;
		foreach ($users as &$user) {
			$users_id = ",".$user['id'];
			$sql  = "{$user['id']}";
			for($i=$who+2;$i<=7;$i++){
				$sql = "select id from user where topuser in ({$sql})";
			}
			if($reddata1 && $reddata2){
				$sql2 = " and orders_id in (select id from orders where order_id in (select id from ordered where datatime >= {$reddata1} and datatime<={$reddata2}) {$cond})";
				$sql = "select * from profzb where uid in ({$sql})".$sql2;

				//查询一共有多少期
				$sql_num = "select * from ordered where datatime >= {$reddata1} and datatime<={$reddata2} group by datatime";
				$num = count(Db::query($sql_num));
				
				$this->assign("num",$num);
				$this->assign("data","期数:{$reddata1}~{$reddata2}");
			}else{
				if($stime){
					$start = strtotime($stime);
				}else{
					$start = 0;
				}

				if($etime){
					$end = strtotime($etime);
				}else{
					$end = time();
				}

				$sql2 = " and orders_id in (select id from orders where order_id in (select id from ordered where times > {$start} and times<{$end}) {$cond})";

				$sql = "select * from profzb where uid in ({$sql})".$sql2;


				//查询一共有多少期
				$sql_num = "select * from ordered where times > {$start} and times<{$end} group by datatime";
				$num = count(Db::query($sql_num));
				
				$this->assign("num",$num);
				$this->assign("data","日期:{$stime}~{$etime}");
			}
			//$sql = "select * from profzb where uid in ({$sql})";
			$orders = Db::query($sql);
			$price = 0;
			$userResult = 0;
			$dlResult = 0;
			$zdlResult = 0;
			$gdResult = 0;
			$dgdResult = 0;
			$htResult = 0;
			$zjResult = 0;
			foreach ($orders as $order) {
				$price += $order['allorder'];
				$userResult+=$order['user7'];
				$dlResult+=$order['user6'];
				$zdlResult+=$order['user5'];
				$gdResult+=$order['user4'];
				$dgdResult+=$order['user3'];
				$zjResult+=$order['user2'];
				$htResult+=$order['user1'];

				$lastPrice +=  $order['user'.$who];
			}
			$user['笔数'] = count($orders);
			$user['下注金额'] = $price;
			$user['会员结果'] = $userResult;
			$user['代理商结果'] = $dlResult;
			$user['总代理结果'] = $zdlResult;
			$user['股东结果'] = $gdResult;
			$user['大股东结果'] = $dgdResult;
			$user['公司损益'] = $htResult;
			$user['总监结果'] = $zjResult;
		}
		$users_id = "(".substr($users_id, 1).")";
		$bhs = db("buhuo")->where("topuser",session('adminid'))->select();
		$price = 0;
		$result = 0;
		foreach ($bhs as $item) {
			$price +=  $item['price'];
			$result += $item['result'];
		}

		$role['补货笔数'] = count($bhs);
		$role['补货金额'] = $price;
		$role['补货损益'] = $result;
		$role['总损益'] = $lastPrice + $result;
		// mp($users);
		$this->assign("users",$users);
		$this->assign("role",$role);
		return $this->fetch();

	}
}